Ext.define('Ext.app.ThemMatHangWindow', {
    extend: 'Ext.window.Window',
    alias: 'widget.themMatHangWindow',
    
    initComponent : function() {
        Ext.getBody().mask();
        
        Ext.apply(this, {
            height: 230,
            width: 450,
            layout: 'fit',
            plain:true,
            prodPK: null,
            items: {
                xtype: 'form',
                layout: 'absolute',
                defaultType: 'textfield',
                border: false,
                items: [{
                    fieldLabel: translations.get("HOME.PRODUCT_CODE"),
                    labelWidth: 130,
                    x: 5,
                    y: 5,
                    name: 'code',
                    allowBlank: false,
                    afterLabelTextTpl: required,
                    anchor: '-5'  // anchor width by percentage
                }, {
                    fieldLabel: translations.get("HOME.XUAT_XU"),
                    labelWidth: 130,
                    x: 5,
                    y: 35,
                    name: 'madeIn',
                    allowBlank: false,
                    afterLabelTextTpl: required,
                    anchor: '-5'  // anchor width by percentage
                }, {
                    fieldLabel: translations.get("HOME.PRODUCT_NAME_BY_SUPPLIER"),
                    labelWidth: 130,
                    x: 5,
                    y: 65,
                    name: 'nameBySupplier',
                    allowBlank: false,
                    afterLabelTextTpl: required,
                    anchor: '-5'  // anchor width by percentage
                }, {
                    fieldLabel: translations.get("HOME.PRODUCT_NAME_FOR_SELL"),
                    labelWidth: 130,
                    x: 5,
                    y: 95,
                    name: 'nameForSell',
                    allowBlank: false,
                    afterLabelTextTpl: required,
                    anchor: '-5'  // anchor width by percentage
                }, {
                    xtype: 'combobox',
                    fieldLabel: translations.get("HOME.LOAI_HANG_HOA"),
                    labelWidth: 130,
                    x: 5,
                    y: 125,
                    name: 'type',
                    displayField: 'name',
                    valueField: 'pk',
                    store: {
                        fields: ['pk', 'name'],
                        proxy: {
                            type: 'ajax',
                            url: 'LoaiHangHoa.jsp',
                            reader: {
                                type: 'json',
                                root: 'root'
                            }
                        }
                    },
                    editable: false,
                    allowBlank: false,
                    afterLabelTextTpl: required,
                    anchor: '-5'  // anchor width by percentage
                }]
            },

            buttons: [{
                text: 'Ok',
                handler: function(button) {
                    var wind = button.up('themMatHangWindow');
                    var form = wind.down('form');
                    if(!form.isValid()) {
                        return;
                    }
                    var params = {
                        'prodPK': wind.prodPK,
                        'code': form.down('[name=code]').getValue(),
                        'madeIn': form.down('[name=madeIn]').getValue(),
                        'nameBySupplier': form.down('[name=nameBySupplier]').getValue(),
                        'nameForSell': form.down('[name=nameForSell]').getValue(),
                        'type': form.down('[name=type]').getValue()
                    };
                    wind.mask(translations.get("HOME.PLEASE_WAIT"));
                    Ext.Ajax.request({
                        url: 'LuuMatHang.jsp',
                        params: params,
                        method: 'POST',
                        success: function(response, opts) {
                            wind.unmask();
                            var obj = Ext.decode(response.responseText);
                            if(!obj.success) {
                                showErrorMsg(obj.msg);
                            }
                            else {
                                wind.opener.reload();
                                wind.close();
                            }
                        },
                        failure: function(response, opts) {
                            wind.unmask();
                            alert('server-side failure with status code ' + response.status);
                        }
                    });
                }
            },{
                text: 'Cancel', 
                handler: function(button) {
                    var wind = button.up('themMatHangWindow');
                    if(wind != null) {
                        wind.close();
                    }
                }
            }]            
        });
        
        this.callParent();
    },
    
    listeners: {
        beforeclose: function () {
            Ext.getBody().unmask();
        }
    },
    
    setOpener: function(opener) {
        this.opener = opener;
    },
    
    setData: function(jsObj) {
        var form = this.down('form');
        this.prodPK = jsObj.prodPK;
        form.down('[name=code]').setValue(jsObj.code);
        form.down('[name=madeIn]').setValue(jsObj.madeIn);
        form.down('[name=nameBySupplier]').setValue(jsObj.nameBySupplier);
        form.down('[name=nameForSell]').setValue(jsObj.nameForSell);
        form.down('[name=type]').setValue(jsObj.type);
    }
});
